# admin.py
from django.contrib import admin
from django.utils.html import format_html
from .models import DjDishCategory, DjDish, DjDishImage, DjDishOption, DjDishOptionChoice

# 菜品分类管理
@admin.register(DjDishCategory)
class DjDishCategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'name_en', 'is_active', 'sort_order', 'image_preview')
    list_editable = ('is_active', 'sort_order')
    list_filter = ('is_active', 'created_at')
    search_fields = ('name', 'name_en')
    ordering = ('sort_order', 'name')
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'name_en', 'description', 'is_active', 'sort_order')
        }),
        ('图片', {
            'fields': ('image', 'image_preview'),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ('image_preview', 'created_at', 'updated_at')
    
    def image_preview(self, obj):
        if obj.image:
            return format_html('<img src="{}" style="max-height: 100px; max-width: 150px;" />', obj.image.url)
        return "-"
    image_preview.short_description = '图片预览'

# 菜品图片内联管理
class DjDishImageInline(admin.TabularInline):
    model = DjDishImage
    extra = 1
    fields = ('image', 'is_primary', 'sort_order', 'image_preview')
    readonly_fields = ('image_preview',)
    
    def image_preview(self, obj):
        if obj.image:
            return format_html('<img src="{}" style="max-height: 80px; max-width: 120px;" />', obj.image.url)
        return "-"
    image_preview.short_description = '预览'

# 菜品选项值内联管理
class DjDishOptionChoiceInline(admin.TabularInline):
    model = DjDishOptionChoice
    extra = 1
    fields = ('name', 'price_adjustment')
    min_num = 1

# 菜品选项内联管理
class DjDishOptionInline(admin.TabularInline):
    model = DjDishOption
    extra = 1
    fields = ('name', 'is_required')
    show_change_link = True
    inlines = [DjDishOptionChoiceInline]

# 菜品管理
@admin.register(DjDish)
class DjDishAdmin(admin.ModelAdmin):
    list_display = ('id','name', 'chef', 'category', 'base_price', 'display_price', 'is_available', 'is_featured', 'created_at')
    list_filter = ('is_available', 'is_featured', 'category', 'chef', 'created_at')
    search_fields = ('name', 'description', 'chef__username','chef__id', 'chef__openid')
    list_editable = ('is_available', 'is_featured', 'base_price')
    readonly_fields = ('display_price', 'created_at', 'updated_at')
    inlines = [DjDishImageInline, DjDishOptionInline]
    filter_horizontal = ('dish_images',)
    fieldsets = (
        ('基本信息', {
            'fields': (
                'chef', 
                'category', 
                'name', 
                'description', 
                ('base_price', 'display_price'),
            )
        }),
        ('服务选项', {
            'fields': (
                'provide_meal_service', 
                'provide_utensils',
            ),
            'classes': ('collapse',)
        }),
        ('菜品属性', {
            'fields': (
                'is_spicy', 
                'is_vegetarian', 
                'prep_time',
            ),
            'classes': ('collapse',)
        }),
        ('状态管理', {
            'fields': (
                'is_available', 
                'is_featured', 
                'sort_order',
            )
        }),
        ('图片管理', {
            'fields': (
                'cloud_image_id',
                'dish_images'
            ),
            'classes': ('collapse',)
        }),
        ('元数据', {
            'fields': (
                'created_at', 
                'updated_at',
            ),
            'classes': ('collapse',)
        }),
    )
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('chef', 'category')

# 菜品图片管理
@admin.register(DjDishImage)
class DjDishImageAdmin(admin.ModelAdmin):
    list_display = ('dish', 'image_preview', 'is_primary', 'is_approved', 'approved_at')
    list_filter = ('is_primary', 'is_approved', 'approved_at')
    search_fields = ('dish__name',)
    list_editable = ('is_primary', 'is_approved')
    readonly_fields = ('image_preview', 'approved_at', 'created_at')
    fields = ('dish', 'image', 'image_preview', 'is_primary', 'sort_order', 'is_approved', 'approved_by', 'approved_at')
    
    def image_preview(self, obj):
        if obj.image:
            return format_html('<img src="{}" style="max-height: 150px; max-width: 200px;" />', obj.image.url)
        return "-"
    image_preview.short_description = '图片预览'
    
    def save_model(self, request, obj, form, change):
        if not change and obj.is_approved:
            obj.approved_by = request.user
        super().save_model(request, obj, form, change)

# 菜品选项管理
@admin.register(DjDishOption)
class DjDishOptionAdmin(admin.ModelAdmin):
    list_display = ('dish', 'name', 'is_required')
    list_filter = ('is_required',)
    search_fields = ('dish__name', 'name')
    inlines = [DjDishOptionChoiceInline]

# 菜品选项值管理
@admin.register(DjDishOptionChoice)
class DjDishOptionChoiceAdmin(admin.ModelAdmin):
    list_display = ('option', 'name', 'price_adjustment')
    list_editable = ('price_adjustment',)
    search_fields = ('option__name', 'name')
    list_filter = ('option__dish',)
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('option__dish')